package com.example.demo.config.event;

import com.example.demo.app.entity.FormEntity;
import com.example.demo.utils.RedisUtil;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;

import com.example.demo.config.enum_.ComplexFormEvents;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@WithStateMachine(name="complexFormMachine")
public class ComplexFormEventConfig {
	/**
     	* 当前状态BLANK_FORM
     */
	@OnTransition(target = "BLANK_FORM")
    public void create(Message<ComplexFormEvents> message) {
        System.out.println("---空白复杂表单---");
    }
    @OnTransition(target ="BLANK_FORM",source= "FULL_FORM")
    public void write(Message<ComplexFormEvents> message) {
    	System.out.println("状态"+message.getPayload().toString()+ "   传递的参数：" + message.getHeaders().get("form") );
        FormEntity entity =  message.getHeaders().get("form",FormEntity.class);
        entity.setStatus(message.getPayload().toString());
        RedisUtil.set(entity.getId(),entity);
        System.out.println("---填写完复杂表单---");
    }
    @OnTransition(source = "FULL_FORM", target = "CHECK_CHOICE")
    public void check(Message<ComplexFormEvents> message) {
    	System.out.println("状态"+message.getPayload().toString()+ "   传递的参数：" +  message.getHeaders().get("form").toString());
        FormEntity entity =  message.getHeaders().get("form",FormEntity.class);
        entity.setStatus(message.getPayload().toString());
        RedisUtil.set(entity.getId(),entity);
    	System.out.println("---校验复杂表单---");
    }
    
    //choice 不会执行此代码
    /**
       * 从CHECK_CHOICE状态如果判断后变成CONFIRM_FORM，
	 * 执行check2confirm方法，但可惜，状态的确变化了，但这个方法不会执行，
	 * 只有在做判断的时候会执行ComplexFormCheckChoiceGuard的evaluate()。
	 * 这就有个问题，在实际运行中，我们的确会需要在choice做出判断状态改变的时候要做些业务处理
	 *	我们完全可以在每个状态变化withChoice的时候独立写一个action，这样的话就能做到业务的互不打扰
     * @param message
     */
    @OnTransition(source = "CHECK_CHOICE", target = "CONFIRM_FORM")
    public void check1CONFIRM(Message<ComplexFormEvents> message) {
    	System.out.println("传递的参数：" + message.getHeaders().get("form").toString());
    	System.out.println("---校验表单到待提交表单(choice true)---");
    }
    
    //choice 不会执行此代码
    @OnTransition(source = "CHECK_CHOICE", target = "DEAL_FORM")
    public void check1DEAL(Message<ComplexFormEvents> message) {
    	System.out.println("传递的参数：" + message.getHeaders().get("form").toString());
    	System.out.println("---校验表单到待提交表单(choice false)---");
    }
    
    @OnTransition(source = "DEAL_FORM", target = "DEAL_CHOICE")
    public void deal(Message<ComplexFormEvents> message) {
    	System.out.println("状态"+message.getPayload().toString()+ "   传递的参数：" + message.getHeaders().get("form") );
        FormEntity entity =  message.getHeaders().get("form",FormEntity.class);
        entity.setStatus(message.getPayload().toString());
        RedisUtil.set(entity.getId(),entity);
    	System.out.println("---处理复杂表单---");
    }
    
    
    //choice 不会执行此代码
	/**
	 * @param message
	 */
    @OnTransition(source = "DEAL_CHOICE", target = "FAILED_FORM")
    public void deal2fail(Message<ComplexFormEvents> message) {
    	System.out.println("传递的参数：" + message.getHeaders().get("form").toString());
    	System.out.println("---处理复杂表单失败(choice false)---");
    }
    
    //choice 不会执行此代码
    @OnTransition(source = "DEAL_CHOICE", target = "FULL_FORM")
    public void deal2full(Message<ComplexFormEvents> message) {
    	System.out.println("传递的参数：" + message.getHeaders().get("form").toString());
    	System.out.println("---处理复杂表单到重新填写表单(choice true)---");
    }
    
    /**
     * CONFIRM_FORM->SUCCESS_FORM 执行的动作
     */
    @OnTransition(source = "CONFIRM_FORM", target = "SUCCESS_FORM")
    public void submit(Message<ComplexFormEvents> message) {
    	System.out.println("状态"+message.getPayload().toString()+ "   传递的参数：" +  message.getHeaders().get("form").toString());
        FormEntity entity = message.getHeaders().get("form",FormEntity.class);
        entity.setStatus(message.getPayload().toString());
        RedisUtil.set(entity.getId(),entity);
    	System.out.println("---表单提交成功---");
    }
}
